<?php
session_start();
if(($_SESSION['datos_admin']['estado'] == "autenticado") && ($_SESSION['datos_admin']['tipo'] == 3)) {

include("header.php");

$mydirname = basename( dirname( __FILE__ ) ) ;

$myts =& MyTextSanitizer::getInstance();
$module_id = $xoopsModule->getVar('mid');

/* if (is_object($xoopsUser)) {
    $groups = $xoopsUser->getGroups();
} else {
	$groups = XOOPS_GROUP_ANONYMOUS;
}

$gperm_handler =& xoops_gethandler('groupperm');

if (isset($_POST['item_id'])) {
    $perm_itemid = intval($_POST['item_id']);
} else {
    $perm_itemid = 0;
}
//If no access
if (!$gperm_handler->checkRight("resume_submit", $perm_itemid, $groups, $module_id)) {
    redirect_header(XOOPS_URL."/modules/$mydirname/index2.php", 3, _NOPERM);
    exit();
} */

/**
 * Funci�n para modificar la informaci�n de registro de una empresa
 *
 * @param int $login_empresa: n�mero de identificaci�n de la empresa
 */
function ModEmpresa($login_empresa)
{

	//global $xoopsDB, $xoopsModule, $xoopsConfig, $xoopsUser, $monnaie, $moderated, $photomax, $ynsalary, $xoopsTheme, $myts, $xoopsLogger;
	global $xoopsDB, $xoopsModule, $xoopsConfig, $xoopsModuleConfig, $xoopsUser, $xoopsTheme, $myts, $xoopsLogger, $mydirname;

	$token = $GLOBALS['xoopsSecurity']->createToken();
	
	$wysiwyg_folder = XOOPS_ROOT_PATH."/class/wysiwyg";
	
	if ( file_exists( $wysiwyg_folder ) && ($xoopsModuleConfig['koivi'] == '1')) {
	include_once XOOPS_ROOT_PATH."/class/xoopsformloader.php";
	include_once XOOPS_ROOT_PATH . "/class/wysiwyg/formwysiwygtextarea.php";
	}else{
	include_once (XOOPS_ROOT_PATH."/include/xoopscodes.php");
	}
	
	echo "<script language=\"javascript\">\nfunction CLA(CLA) { var MainWindow = window.open (CLA, \"_blank\",\"width=500,height=300,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no\");}\n</script>";

	include_once(XOOPS_ROOT_PATH."/class/xoopstree.php");
		
		
// Detecci�n de errores		
		echo "<script type=\"text/javascript\">


function validarEmail(valor) {
 re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
    if(!re.exec(valor))    {
        return false;
    }else{
        return true;
    }
}

          function verify() {
		   var a = document.add.email.value;
           var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+.[A-Za-z0-9_.]+[A-za-z]$/;
		  
                var msg = \""._VALIDAR_EMPRESAS."\\n__________________________________________________\\n\\n\";
                var errors = \"FALSE\";
              
			    if (document.add.empresa.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDEMPRESA."\\n\";
                }    
                if (document.add.telefono.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDTELEFONO."\\n\";
                }
				if (document.add.direccion.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDDIRECCION."\\n\";
                }
				if (document.add.ciudad.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDCIUDAD."\\n\";
                }
				if (document.add.sector.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDSECTOR."\\n\";
                }
				if (document.add.actividad.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDACT."\\n\";
                }
				if (document.add.representante.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDREP."\\n\";
                }
				if (document.add.cargo.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDCARGOREP."\\n\";
                }
				if (document.add.email.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDREPEMAIL."\\n\";
                }
				if (a.length != 0) {     
                  if (!validarEmail(a)) {
			           errors = \"TRUE\";
                       msg += \""._EMAIL_ERROR."\\n\";
			     }
			 }    
				
                if (errors == \"TRUE\") {
                        msg += \"__________________________________________________\\n\\n"._VALIDAR_EMPRESAS2."\\n\";
                        alert(msg);
                        return false;
                }
          }
          </script>";
		
		echo "<script type=\"text/javascript\">
	 function numeros(e) {
    tecla = (document.all) ? e.keyCode : e.which; 
    if (tecla==8) return true; 
    patron =/\d/; 
    te = String.fromCharCode(tecla); 
    return patron.test(te);
    } 
      </script>"; 
		
    $result = $xoopsDB->query("select login, password, empresa, telefono, indicativo, ext, direccion, fax, indfax, ciudad, sector, actividad, representante, cargo, email from ".$xoopsDB->prefix("jobs_empresas")." where login=".$login_empresa."");
    list($login, $password, $empresa, $telefono, $indicativo, $ext, $direccion, $fax, $indfax, $ciudad, $sector, $actividad, $representante, $cargo, $email) = $xoopsDB->fetchRow($result);
	   
	////////////   
	// modificar solo la informacion correspondiente a la empresa que inicia la sesi�n
    if ($_SESSION['datos_admin']['user'] == $login){
	//////////////
	   
	/* if ($xoopsUser) {
		$calusern = $xoopsUser->uid();
		if ($usid == $calusern) { */

			echo "<fieldset><legend style='font-weight: bold; color: #3366CC;'>"._ACT_EMPRESA."</legend>";
			//echo "<b>"._JOBS_MODIFANN."</b><br /><br />";
	
	$login = $myts->makeTboxData4Save($login);
	$password = $myts->makeTboxData4Save($password);
	$empresa = $myts->makeTboxData4Save($empresa);
	$telefono = $myts->makeTboxData4Save($telefono);
	$indicativo = $myts->makeTboxData4Save($indicativo);
	$ext = $myts->makeTboxData4Save($ext);
	$direccion = $myts->makeTboxData4Save($direccion);
	$fax = $myts->makeTboxData4Save($fax);
	$indfax = $myts->makeTboxData4Save($indfax);
	$ciudad = $myts->makeTboxData4Save($ciudad);
	$sector = $myts->makeTboxData4Save($sector);
	$actividad = $myts->makeTboxData4Save($actividad);
	$representante = $myts->makeTboxData4Save($representante);
	$cargo = $myts->makeTboxData4Save($cargo);
	$email = $myts->makeTboxData4Save($email);
		
			$useroffset = "";
		    if($xoopsUser) {
				$timezone = $xoopsUser->timezone();
				if(isset($timezone)){
					$useroffset = $xoopsUser->timezone();
				}else{
					$useroffset = $xoopsConfig['default_TZ'];
				}
			}
			
		
		   	echo "<form action=\"modempresa.php\" method=\"post\" enctype=\"multipart/form-data\" name=\"add\" onsubmit=\"return verify();\">";
		
           echo "<input type=\"hidden\" name=\"login\" value=\"$login\"/>
		        <input type=\"hidden\" name=\"password\" value=\"$password\"/>				
			    <b>"._INFO_SOLICITANTE."
			    <table width='100%' class='outer' cellspacing='1'><tr>
				<td width='35%' class='odd'>"._NIT_EMPRESA." </td><td class='odd'>$login</td>
				</tr><tr>
				<td width='35%' class='even'>"._NOMBRE_EMPRESA." </td><td class='even'><input type=\"text\" name=\"empresa\" size=\"50\" maxlength=\"100\" value=\"$empresa\" /></td>
				</tr><tr>
				<td width='35%' class='odd'>"._TELEFONO_EMPRESA." </td><td class='odd'><input type=\"text\" name=\"indicativo\" size=\"5\" maxlength=\"5\" value=\"$indicativo\" onKeypress=\"return numeros(event);\"/>
				- &nbsp;<input type=\"text\" name=\"telefono\" size=\"20\" maxlength=\"20\" value=\"$telefono\" onKeypress=\"return numeros(event);\"/>&nbsp;"._EXT."&nbsp;<input type=\"text\" name=\"ext\" size=\"5\" maxlength=\"5\" value=\"$ext\" onKeypress=\"return numeros(event);\"/></td>
				</tr><tr>
				<td width='35%' class='even'>"._FAX_EMPRESA." </td><td class='even'><input type=\"text\" name=\"indfax\" size=\"5\" maxlength=\"5\" value=\"$indfax\" onKeypress=\"return numeros(event);\"/>
				- &nbsp;<input type=\"text\" name=\"fax\" size=\"20\" maxlength=\"20\" value=\"$fax\" onKeypress=\"return numeros(event);\"/></td>
				</tr><tr>
				<td width='35%' class='odd'>"._CIUDAD_EMPRESA." </td><td class='odd'><input type=\"text\" name=\"ciudad\" size=\"50\" maxlength=\"50\" value=\"$ciudad\"/></td>
				</tr><tr>
				<td width='35%' class='even'>"._DIRECCION_EMPRESA." </td><td class='even'><input type=\"text\" name=\"direccion\" size=\"50\" maxlength=\"50\" value=\"$direccion\"/></td>
				</tr><tr>
				<td width='35%' class='odd'>"._SECTOR_EMPRESA." </td><td class='odd'>
				<select name=\"sector\">";
				
				$sectorlist = array(_SECTOR1,
				                    _SECTOR7,
				                    _SECTOR2,
				                    _SECTOR3,
				                    _SECTOR4,
				                    _SECTOR5,
									_SECTOR6,
				);
				
				foreach ($sectorlist as $value){
				    echo "<option value=\"{$value}\" ";
				    if ($value == $sector) {
				    	$selected = "selected";
				    }else {
				        $selected = "";
				    }
				    echo "{$selected}>{$value}</option>";
				}
				
		        echo "</select></td>			

				</tr><tr>
				<td width='35%' class='even'>"._ACTIVIDAD_EMPRESA." </td><td class='even'><input type=\"text\" name=\"actividad\" size=\"50\" maxlength=\"100\" value=\"$actividad\"/></td>
				</tr></table><br />
				
			    <table width='100%' class='outer' cellspacing='1'>
				<b><br>"._INFO_RESPONSABLE." <tr>
				<td width='35%' class='odd'>"._REPRESENTANTE_EMPRESA." </td><td class='odd'><input type=\"text\" name=\"representante\" size=\"50\" maxlength=\"50\" value=\"$representante\"/></td>
				</tr><tr>
				<td width='35%' class='even'>"._CARGO_REPRESENTANTE." </td><td class='even'><input type=\"text\" name=\"cargo\" size=\"50\" maxlength=\"100\" value=\"$cargo\"/></td>
				</tr><tr>
				<td width='35%' class='odd'>"._CORREO_EMPRESA." </td><td class='odd'><input type=\"text\" name=\"email\" size=\"50\" maxlength=\"50\" value=\"$email\"/></td>
				</tr></table>";
		
			echo "<br /><br />
			<table width='100%' class='outer' cellspacing='1'><tr>
			<td colspan=2><br><center><input type=\"submit\" value=\""._CAMBIAR."\">
			<input name=\"btncancelar\" type=\"button\" id=\"btncancelar\" onClick=\"self.location.href='principalempresas.php'\"  value=\""._CANCELAR."\"></center>
			</td></tr></table>";
			echo "<input type=\"hidden\" name=\"op\" value=\"ModEmpresaS\" />";	
		    echo "<input type=\"hidden\" name=\"token\" value=\"$token\" />";
	    	echo "</form><br />";
		echo "</fieldset><br />"; 
    		//CloseTable();
   /*      }    
	} */
	}
	else{
	 echo "<script type=\"text/javascript\">
 history.go(-1);
 </script>";
	}
}

/**
 * Funci�n para actualizar los datos de la empresa en la tabla 'jobs_empresas'
 *
 * @param int $login
 * @param string $password
 * @param string $empresa
 * @param string $telefono
 * @param string $indicativo
 * @param string $ext
 * @param string $direccion
 * @param string $fax
 * @param string $indfax
 * @param string $ciudad
 * @param string $sector
 * @param string $actividad
 * @param string $representante
 * @param string $cargo
 * @param string $email
 */
function ModEmpresaS($login, $password, $empresa, $telefono, $indicativo, $ext, $direccion, $fax, $indfax, $ciudad, $sector, $actividad, $representante, $cargo, $email)
{
	global $xoopsDB, $xoopsConfig, $xoopsConfig, $xoopsModuleConfig, $myts, $xoopsLogger, $mydirname;

		if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['token'])) {
		   redirect_header(XOOPS_URL."/modules/$mydirname/principalempresas.php", 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
		}
			
	
	$login = $myts->makeTboxData4Save($login);
	$password = $myts->makeTboxData4Save($password);
	$empresa = $myts->makeTboxData4Save($empresa);
	$telefono = $myts->makeTboxData4Save($telefono);
	$indicativo = $myts->makeTboxData4Save($indicativo);
	$ext = $myts->makeTboxData4Save($ext);
	$direccion = $myts->makeTboxData4Save($direccion);
	$fax = $myts->makeTboxData4Save($fax);
	$indfax = $myts->makeTboxData4Save($indfax);
	$ciudad = $myts->makeTboxData4Save($ciudad);
	$sector = $myts->makeTboxData4Save($sector);
	$actividad = $myts->makeTboxData4Save($actividad);
	$representante = $myts->makeTboxData4Save($representante);
	$cargo = $myts->makeTboxData4Save($cargo);
	$email = $myts->makeTboxData4Save($email);

	
//actualiza la tabla "jobs_empresas"	
    $xoopsDB->query("update ".$xoopsDB->prefix("jobs_empresas")." set login='$login', password='$password', empresa='$empresa', telefono='$telefono', indicativo='$indicativo', ext='$ext', direccion='$direccion', fax='$fax', indfax='$indfax', ciudad='$ciudad', sector='$sector', actividad='$actividad', representante='$representante', cargo='$cargo', email='$email' where login=$login");
    redirect_header("principalempresas.php",3, _EMP_MOD);
	exit();
}

####################################################
foreach ($HTTP_POST_VARS as $k => $v) {
	${$k} = $v;
}

/* $ok = isset( $_GET['ok'] ) ? $_GET['ok'] : '' ; */

if(!isset($_POST['login_empresa']) && isset($_GET['login_empresa']) ) {
	$login_empresa = $_GET['login_empresa'] ;
}
if(!isset($_POST['op']) && isset($_GET['op']) ) {
	$op = $_GET['op'] ;
}

switch ($op) {

    case "ModEmpresa":
	include(XOOPS_ROOT_PATH."/header.php");
    ModEmpresa($login_empresa);
    
    echo "<br /><br />
<div align=\"center\">" . _INFO_PAG . "</div>
<br /><br />";
    
    
	include(XOOPS_ROOT_PATH."/footer.php");
    break;
	
	case "ModEmpresaS":
    ModEmpresaS($login, $password, $empresa, $telefono, $indicativo, $ext, $direccion, $fax, $indfax, $ciudad, $sector, $actividad, $representante, $cargo, $email);
    break;

    default:
	redirect_header("principalempresas.php",1,""._RETURNANN."");
	break;
}
 }else
{
// Usuario que no se ha logueado
echo "No tienes permiso para entrar a esta p�gina...";
exit();
} 
?>
